<template>
  <Header :check="3" @getUserInfo="getUserInfo" />
  <div class="clear_nav"></div>
  <div id="mine">
    <div class="top">
      <div>
        <p>{{ $t("hl_totalInvest") }}:</p>
        <h5>{{ investAmount }} USDT</h5>
      </div>
      <div>
        <p>{{ $t("hl_totalIncome") }}:</p>
        <h5>{{ getAmount }} USDT</h5>
      </div>
      <div>
        <p>{{ $t("hl_lastIncome") }}:</p>
        <h5>{{ Number(outAmount) - Number(getAmount) }} USDT</h5>
      </div>
      <div>
        <p>直推业绩:</p>
        <h5>{{ Number(userPushAmount) }} USDT</h5>
      </div>
      <div>
        <p>{{ $t("hl_inviter") }}:</p>
        <h5
          style="
            white-space: pre-wrap;
            word-break: break-all;
            word-wrap: break-word;
            flex: 1;
          "
        >
          {{
            leader.slice(0, 5) +
            "..." +
            leader.slice(leader.length - 4, leader.length)
          }}
        </h5>
      </div>
      <div class="copy" v-if="isEfficient">
        <p>{{ $t("hl_myLink") }}:</p>
        <h5>
          {{ inviteUrl }}
        </h5>
        <img
          src="../assets/img/mine/qrcode.png"
          alt=""
          @click="creatQrCode()"
        />
        <h6 @click="copy(inviteUrl)">{{ $t("hl_copy") }}</h6>
      </div>
    </div>

    <div class="coin" v-if="systemPartner">
      <div class="btn">
        <h5>分配</h5>
        <h4 @click="clickShare(1)">合伙人奖池分配</h4>
        <h4 @click="clickShare(2)">推荐奖池分配</h4>
      </div>
    </div>

    <div class="coin">
      <div class="head">
        <div class="left">
          <img src="../assets/img/nav/logo.png" alt="" />
          <p>BOSS</p>
        </div>
        <div>
          <p>{{ parseFloat(coinBalance).toFixed(4) }}</p>
          <p>{{ $t("hl_walletBalance") }}</p>
        </div>
      </div>
      <div class="btn">
        <p @click="router.push('/trade')">{{ $t("hl_sell") }}</p>
        <p
          v-if="
            address ==
            '0x61441D12AD43DDeA12249857A2edaE7b60030721'.toLowerCase()
          "
          @click="showBuy = true"
        >
          {{ $t("hl_buy") }}
        </p>
        <p v-if="state" @click="showMustSell = true">
          {{ $t("hl_activeIncome") }}
        </p>
      </div>
    </div>

    <div class="coin">
      <div class="head">
        <div class="left">
          <img src="../assets/img/nav/logoT.png" alt="" />

          <div>
            <p>BOSST</p>
            <p>
              ( <i>P{{ bossTlevel }}</i> )
            </p>
          </div>
        </div>
        <div>
          <p>{{ parseFloat(canGetBossT).toFixed(4) }}</p>
          <p>可领余额</p>
        </div>
        <div>
          <p>{{ parseFloat(bossTBalance).toFixed(4) }}</p>
          <p>{{ $t("hl_walletBalance") }}</p>
        </div>
      </div>
      <div class="btn">
        <p @click="getBossT()">领取</p>
        <p @click="updataBossT()">
          {{ $t("hl_upGrade") }}
        </p>
      </div>
    </div>

    <div class="coin" v-if="systemPartner">
      <div class="inputA">
        <div class="left_input" style="margin-bottom: 12px">
          <textarea
            v-model="inputAddr"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p v-if="systemPartner" @click="goActive()" style="padding: 0 20px">
            {{ $t("hl_active") }}
            <!-- ({{ Number(availableActivation) }}) -->
          </p>
        </div>
      </div>
      <div class="btn2">
        <p
          :class="activeIndex == 0 ? 'b_sel' : 'b_un'"
          @click="activeIndex = 0"
        >
          20
        </p>
        <p
          :class="activeIndex == 1 ? 'b_sel' : 'b_un'"
          @click="activeIndex = 1"
        >
          100
        </p>
        <p
          :class="activeIndex == 2 ? 'b_sel' : 'b_un'"
          @click="activeIndex = 2"
        >
          1000
        </p>
        <p
          :class="activeIndex == 3 ? 'b_sel' : 'b_un'"
          @click="activeIndex = 3"
        >
          3000
        </p>
      </div>
    </div>

    <div class="coin" v-if="systemPartner">
      <div class="inputA">
        <div class="left_input">
          <textarea
            v-model="inputAddr_partner"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p v-if="systemPartner" @click="partnerClick()">
            {{ $t("hl_uptoPartner") }}
          </p>
        </div>
      </div>
    </div>

    <!-- 设置推荐地址数组   设置合伙人分红地址数组   -->

    <div class="coin" v-if="systemPartner">
      <div class="inputA" style="margin-bottom: 20px">
        <div class="left_input">
          <h4>设置推荐地址</h4>
          <textarea
            v-model="setPushInput"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p style="padding: 0 20px" @click="clickSetPush()">设置</p>
        </div>
      </div>
      <div class="inputA">
        <div class="left_input">
          <h4>合伙人分红地址</h4>
          <textarea
            v-model="setPartnerInput"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p style="padding: 0 20px" @click="clickSetPartner()">设置</p>
        </div>
      </div>
    </div>

    <!-- 赠送节点 -->
    <div class="coin" v-if="systemPartner">
      <!-- <div class="coin"> -->
      <div class="inputA">
        <div class="left_input">
          <textarea
            v-model="inputSendAddr"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p style="padding: 0 20px" @click="setNodeClick()">设置</p>
        </div>
      </div>
      <div class="inputA">
        <div class="left_input">
          {{ $t("hl_persent") }}:
          <input
            style="width: 120px"
            type="Number"
            :placeholder="input1_100"
            v-model="inputAddr_num"
          />
          %
        </div>
      </div>
    </div>

    <!-- 赠送等级 -->
    <div class="coin" v-if="systemPartner">
      <div class="inputA" style="margin-bottom: 20px">
        <div class="left_input">
          <h4>赠送等级(T)</h4>
          <textarea
            v-model="setLevelAddr"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p style="padding: 0 20px" @click="upLevel(-1)">
            {{ $t("hl_upGrade") }}
          </p>
        </div>
      </div>
    </div>
    <!-- 赠送等级 -->
    <div class="coin" v-if="systemPartner">
      <div class="inputA" style="margin-bottom: 20px">
        <div class="left_input">
          <h4>赠送等级(P)</h4>
          <textarea
            v-model="setPAddr"
            name=""
            id=""
            cols="30"
            rows="10"
            :placeholder="$t('hl_inputOpAddr')"
          ></textarea>
        </div>
        <div>
          <p style="padding: 0 20px" @click="upLevel(-2)">
            {{ $t("hl_upGrade") }}
          </p>
        </div>
      </div>
    </div>

    <div class="list">
      <h1>{{ $t("hl_sugAddr") }}</h1>

      <template v-if="inviteList.length">
        <div class="cell" v-for="(item, index) in inviteList" :key="index">
          <div class="line1">
            <div class="addr">
              <p>
                {{
                  item.address.slice(0, 5) +
                  "..." +
                  item.address.slice(
                    item.address.length - 4,
                    item.address.length
                  )
                }}
              </p>
              <div style="display: flex; align-items: center">
                <!-- <img src="../assets/img/nav/vip.png"
                     alt="" /> -->
                <i style="font-weight: bold">T{{ item.level_id }}</i>
              </div>
            </div>
            <div class="funBtn">
              <p v-if="systemPartner" @click="upLevel(index)">
                {{ $t("hl_upGrade") }}
              </p>
              <!-- <h5 v-if="item.is_valid == 0">
                {{ $t("hl_noVIP") }}
              </h5> -->
              <!-- <h5
                v-if="availableActivation > 0 && item.is_valid == 0"
                @click="goActive(index)"
              >
                {{ $t("hl_active") }}
              </h5> -->
            </div>
          </div>
          <div class="line1">
            <h6>{{ item.timestamp }}</h6>

            <!-- <h5
              v-if="systemPartner && item.id == -1"
              @click="partnerClick(index)"
            >
              {{ $t("hl_uptoPartner") }}
            </h5> -->
          </div>
        </div>
      </template>
      <template v-else>
        <div class="nodata">
          <img src="../assets/img/mine/nodata.png" />
          <p>{{ $t("hl_noData") }}</p>
        </div>
      </template>
    </div>

    <template v-if="showLevelUp">
      <div class="level_pop">
        <van-popup v-model:show="showLevelUp" round :style="{ width: '90%' }">
          <img
            src="../assets/img/nav/close.png"
            class="back"
            alt=""
            @click="showLevelUp = false"
          />
          <div class="level">
            <h1>{{ $t("hl_upGrade") }}</h1>
            <h5>{{ $t("hl_upLevel") }}:</h5>
            <div class="choose" :class="showLevelSelect ? 'sel_1' : 'sel_2'">
              <p @click="showLevelSelect = !showLevelSelect">
                {{ levelList[levelIndex].name }}
              </p>
              <img
                src="../assets/img/nav/down.png"
                alt=""
                @click="showLevelSelect = !showLevelSelect"
              />
              <div class="selectList" v-if="showLevelSelect">
                <div
                  class="sel_cell"
                  v-for="(item, index) in levelList"
                  :key="index"
                >
                  <h6 @click="funSelLevel(index)">{{ item.name }}</h6>
                </div>
              </div>
            </div>
            <div class="levelBtn">
              <h5 @click="showLevelUp = false">{{ $t("hl_cancle") }}</h5>
              <p @click="confirmClick()">{{ $t("hl_confirm") }}</p>
            </div>
          </div>
        </van-popup>
      </div>
    </template>

    <template v-if="showBuy">
      <div class="level_pop">
        <van-popup v-model:show="showBuy" round :style="{ width: '90%' }">
          <img
            src="../assets/img/nav/close.png"
            class="back"
            alt=""
            @click="showBuy = false"
          />
          <div class="level">
            <h1>{{ $t("hl_buy") }}</h1>
            <h5>{{ $t("hl_inputBuy") }}:</h5>
            <input
              type="Number"
              v-model="inputBuy"
              :placeholder="$t('hl_inputBuy')"
            />
            <!-- <h4>
              {{ $t("hl_canOutAmount") }}:
              {{ parseFloat(balanceOf).toFixed(4) }}
            </h4> -->
            <div class="levelBtn">
              <h5 @click="showBuy = false">{{ $t("hl_cancle") }}</h5>
              <p @click="goBuy()">{{ $t("hl_confirm") }}</p>
            </div>
          </div>
        </van-popup>
      </div>
    </template>

    <template v-if="showMustSell">
      <div class="level_pop">
        <van-popup v-model:show="showMustSell" round :style="{ width: '90%' }">
          <div class="level">
            <h1>{{ $t("hl_node4") }}</h1>
            <h3>
              {{ $t("hl_node5") }}
            </h3>

            <div class="levelBtn">
              <h5 @click="showMustSell = false">{{ $t("hl_cancle") }}</h5>
              <p @click="goSell()">{{ $t("hl_confirm") }}</p>
            </div>
          </div>
        </van-popup>
      </div>
    </template>

    <van-popup
      v-model:show="showQRcode"
      round
      :style="{ width: '200px', height: '240px' }"
    >
      <div class="level_pop">
        <img
          src="../assets/img/nav/close.png"
          class="back"
          alt=""
          @click="showQRcode = false"
        />
        <div class="urlText">{{ $t("hl_scanBind") }}</div>
        <div class="qrcodeView" ref="qrCodeUrl" id="qrcode"></div>
      </div>
    </van-popup>
  </div>
</template>

<script setup>
// import { api_inviteRecord, api_getWallet, api_getConfig } from "../request/api";
import { ref, reactive, watch } from "vue";
import {
  showFailToast,
  showSuccessToast,
  showLoadingToast,
  closeToast,
  showToast,
} from "vant";
import {
  getFloat,
  getDotcAllowance,
  getCtcAllowance,
  getEstimateGas,
} from "../utils/common";
import { useRouter } from "vue-router";
import i18n from "@/lang";
import api from "../dapp/api";
import QRCode from "qrcodejs2";
import useClipboard from "vue-clipboard3"; // 复制组件
const { toClipboard } = useClipboard();

const router = useRouter();

let address = ref("");
let inputAddr = ref("");
let inputAddr_partner = ref("");
let activeIndex = ref(0);
let activeIndexArr = ref([20, 100, 1000, 3000]);
let inputAddr_num = ref("");
let showLevelUp = ref(false);
let showLevelSelect = ref(false);
let levelIndex = ref(0);
let showWithdraw = ref(false);
let showBuy = ref(false);
let showQRcode = ref(false);
let inputSendAddr = ref("");
let levelList = ref([]);
let showMustSell = ref(false);
let setPushInput = ref("");
let setPartnerInput = ref("");
let setLevelAddr = ref("");
let setPAddr = ref("");

//     uint256 outAmount;  // 出局金额
//     uint256 getAmount;  // 已经获得的金额
//     uint256 investAmount;  // 投资的金额
//     bool partner;  // 是否是合伙人
//     bool systemPartner;  // 是否是系统赠送合伙人
//     uint256 availableActivation;  //剩余激活数量
let userPushAmount = ref(0);
let outAmount = ref(0);
let getAmount = ref(0);
let investAmount = ref(0);
let availableActivation = ref(0);
let leader = ref("");
let inviteUrl = ref("");
let inputWithdraw = ref("");
let inputBuy = ref("");
let myLevel = ref(0);
let isEfficient = ref(false);
let coinPrice = ref(0);
let nodeNum = ref(100);

let conContract, coinContract, usdtContract, allowContract, bossTContract, web3;
let coinBalance = ref(0);
// let web3 = ref(null);
let systemPartner = ref(false);
let state = ref(false);
let buyAmount = ref(0);
let usdtBalance = ref(0);
let bossTBalance = ref(0);
let canGetBossT = ref(0);
let bossTlevel = ref(0);
let input1_100 = ref("");
let gasPrice = ref(0);
// 那得的数据
function getUserInfo(obj) {
  address.value = obj.address.toLowerCase();
  isEfficient.value = obj.userData.isEfficient;
  web3 = obj.web3;

  systemPartner.value = obj.userData.systemPartner;

  conContract = new obj.web3.eth.Contract(conAbi, api.conAddress);
  coinContract = new obj.web3.eth.Contract(ctcAbi, api.coinAddress);
  usdtContract = new obj.web3.eth.Contract(tongAbi, api.usdtAddress);
  allowContract = new obj.web3.eth.Contract(allowanceAbi, api.allowanceAddress);
  bossTContract = new obj.web3.eth.Contract(tongAbi, api.coinTAddress);
  outAmount.value = obj.web3.utils.fromWei(obj.userData.outAmount, "ether");
  getAmount.value = obj.web3.utils.fromWei(obj.userData.getAmount, "ether");
  investAmount.value = obj.web3.utils.fromWei(
    obj.userData.investAmount,
    "ether"
  );
  availableActivation.value = 1600;
  gasPrice.value = obj.gasPrice;

  leader.value = obj.userData.leader;
  myLevel.value = obj.userData.level;
  bossTlevel.value = obj.userData.PPP;

  inviteUrl.value = api.inviteUrl + "?p_address=" + address.value;

  conContract.methods
    .getUstate(address.value)
    .call()
    .then(async (res) => {
      state.value = !Boolean(res);
      showMustSell.value = state.value;
    });

  conContract.methods
    .getTokenPrice()
    .call()
    .then(async (res) => {
      coinPrice.value = parseFloat(Number(res) / 10000).toFixed(4);
    });

  conContract.methods
    .getUserRecommends(address.value, 1, 0)
    .call()
    .then(async (res) => {
      if (res.length) {
        checkLevel(res);
      }
    });

  conContract.methods
    .getUserredirects600(address.value)
    .call()
    .then(async (res) => {
      userPushAmount.value = obj.web3.utils.fromWei(res, "ether");
    });

  getBalance();
}

function getBalance() {
  conContract.methods
    .getbosstoken(address.value)
    .call()
    .then(async (res) => {
      canGetBossT.value = web3.utils.fromWei(res, "ether");
    });
  coinContract.methods
    .balanceOf(address.value)
    .call()
    .then(async (res) => {
      coinBalance.value = web3.utils.fromWei(res, "ether");
    });
  usdtContract.methods
    .balanceOf(address.value)
    .call()
    .then(async (res) => {
      usdtBalance.value = web3.utils.fromWei(res, "ether");
    });

  bossTContract.methods
    .balanceOf(address.value)
    .call()
    .then(async (res) => {
      bossTBalance.value = web3.utils.fromWei(res, "ether");
    });
}

async function checkLevel(list) {
  let arr = [];
  for (let index = 0; index < list.length; index++) {
    const element = list[index];

    let level_id = "";

    await conContract.methods
      .getUserInfo(element)
      .call()
      .then(async (res) => {
        level_id = Number(res.level);
      });

    let dic = {
      address: element,
      level_id: level_id,
    };
    arr.push(dic);
  }

  inviteList.value = arr;
}

watch(inputAddr, (newValue, oldValue) => {
  if (newValue.length == 42) {
    searchPartner();
    showLoadingToast({
      message: i18n.global.t("gg_loading"),
      forbidClick: true,
      duration: 3000,
    });
  } else {
    isEfficientFlag.value = false;
    partnerFlag.value = false;
  }
});

function funSelLevel(index) {
  levelIndex.value = index;
  showLevelSelect.value = false;
}

const copy = async (url) => {
  if (url != null && url != "") {
    await toClipboard(url);
    showToast(i18n.global.t("copy_suc"));
  }
};

let inviteList = ref([]);
function goActive() {
  if (inputAddr.value.length != 42) {
    showFailToast(i18n.global.t("hl_inputTrueAddr"));
    return;
  }

  // if (!isEfficientFlag.value) {
  //   showFailToast(i18n.global.t("hl_isActive"));
  //   return;
  // }
  try {
    conContract.methods
      .giveactivation(
        inputAddr.value,
        Number(activeIndexArr.value[activeIndex.value])
      )
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

let currentSelectUser = ref(0);
function upLevel(index) {
  showLevelUp.value = true;

  let userLevel = 0;
  currentSelectUser.value = index;
  let n = "";
  if (index == -1) {
    userLevel = 0;
    n = "T";
  } else if (index == -2) {
    userLevel = 0;
    n = "P";
  } else {
    userLevel = inviteList.value[index].level_id;
    n = "T";
  }

  levelList.value = [];
  for (let index = 0; index < 13 - Number(userLevel); index++) {
    let item = {
      name: n + (userLevel + index + 1),
      lIndex: userLevel + index + 1,
    };
    levelList.value.push(item);
  }
}

function confirmClick() {
  if (showLevelSelect.value) {
    showLevelSelect.value = false;
    return;
  }
  if (!levelList.value.length) {
    showFailToast(i18n.global.t("hl_noUpLevel"));
    return;
  }

  let toLevel = levelList.value[levelIndex.value].lIndex;
  let addr = "";
  if (currentSelectUser.value == -1) {
    if (setLevelAddr.value.length != 42) {
      showFailToast(i18n.global.t("hl_inputTrueAddr"));
      return;
    }

    addr = setLevelAddr.value;
  } else if (currentSelectUser.value == -2) {
    if (setPAddr.value.length != 42) {
      showFailToast(i18n.global.t("hl_inputTrueAddr"));
      return;
    }

    addr = setPAddr.value;
  } else {
    addr = inviteList.value[currentSelectUser.value].address;
  }

  if (currentSelectUser.value == -2) {
    try {
      conContract.methods
        .givePLevel(addr, toLevel)
        .send({
          from: address.value,
          gas: 600000,
          gasPrice: gasPrice.value,
        })
        .on("transactionHash", (hash) => {
          showLoadingToast({
            message: i18n.global.t("hl_waitLink"),
            forbidClick: true,
            duration: 0,
          });
        })
        .on("receipt", (receipt) => {
          closeToast();
          showSuccessToast(i18n.global.t("hl_opSucc"));
          setTimeout(() => {
            closeToast();
            location.reload();
          }, 2500);
        })
        .on("error", (error, receipt) => {
          closeToast();
          showFailToast(i18n.global.t("hl_opFail"));
        });
    } catch (e) {
      console.log("e :>> ", e);
    }
  } else {
    try {
      conContract.methods
        .giveLevel(addr, toLevel)
        .send({
          from: address.value,
          gas: 600000,
          gasPrice: gasPrice.value,
        })
        .on("transactionHash", (hash) => {
          showLoadingToast({
            message: i18n.global.t("hl_waitLink"),
            forbidClick: true,
            duration: 0,
          });
        })
        .on("receipt", (receipt) => {
          closeToast();
          showSuccessToast(i18n.global.t("hl_opSucc"));
          setTimeout(() => {
            closeToast();
            location.reload();
          }, 2500);
        })
        .on("error", (error, receipt) => {
          closeToast();
          showFailToast(i18n.global.t("hl_opFail"));
        });
    } catch (e) {
      console.log("e :>> ", e);
    }
  }
}

// let withdraw_min = ref(10);

let partnerFlag = ref(false);
let isEfficientFlag = ref(false);
function searchPartner() {
  if (inputAddr.value.length != 42) {
    return;
  }
  conContract.methods
    .getUserInfo(inputAddr.value)
    .call()
    .then(async (res) => {
      console.log("addressInfo=", res);
      closeToast();
      if (res.partner == false) {
        partnerFlag.value = true;
      }
      if (res.isEfficient == false) {
        isEfficientFlag.value = true;
      }
    });
}

function partnerClick() {
  if (inputAddr_partner.value.length != 42) {
    showFailToast(i18n.global.t("hl_inputTrueAddr"));
    return;
  }

  if (!partnerFlag.value) {
    showFailToast(i18n.global.t("hl_ispartner"));
    return;
  }

  try {
    conContract.methods
      .systemPartnergivepartner(inputAddr_partner.value)
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

const qrCodeUrl = ref(null);
let qrFlag = ref(false);
function creatQrCode() {
  if (!inviteUrl.value) {
    return;
  }

  showQRcode.value = true;
  if (qrFlag.value) {
    return;
  }
  setTimeout(() => {
    var qrcode = new QRCode(qrCodeUrl.value, {
      text: inviteUrl.value, // 需要转换为二维码的内容
      width: 120,
      height: 120,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });

    qrFlag.value = true;
  }, 500);
}

async function goSell() {
  await getCtcAllowance(coinContract, allowContract, api.conAddress);
  let str = parseFloat(coinBalance.value) / 2;
  try {
    conContract.methods
      .sell(web3.utils.toWei(str.toString(), "ether"))
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

async function goBuy() {
  if (Number(inputBuy.value) > Number(usdtBalance.value)) {
    showFailToast(i18n.global.t("hl_noMoney"));
    return;
  }
  await getDotcAllowance(address.value, usdtContract, api.conAddress);
  try {
    conContract.methods
      .buy_Owner(web3.utils.toWei(inputBuy.value, "ether"))
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

function clickShare(index) {
  let TwoTime = Number(sessionStorage.getItem("t_clickShare"));
  let news = new Date().getTime();
  if (news - TwoTime < 2000) {
    return;
  }
  sessionStorage.setItem("t_clickShare", new Date().getTime());

  if (index == 1) {
    conContract.methods
      .partnerpoolAllocation()
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } else {
    conContract.methods
      .recommendpoolAllocation()
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  }
}

function clickSetPush() {
  if (setPushInput.value.length != 42) {
    showFailToast(i18n.global.t("hl_inputTrueAddr"));
    return;
  }

  let arr = [];
  arr.push(setPushInput.value);
  conContract.methods
    .setrecommendAddress(arr)
    .send({
      from: address.value,
      gas: 600000,
      gasPrice: gasPrice.value,
    })
    .on("transactionHash", (hash) => {
      showLoadingToast({
        message: i18n.global.t("hl_waitLink"),
        forbidClick: true,
        duration: 0,
      });
    })
    .on("receipt", (receipt) => {
      closeToast();
      showSuccessToast(i18n.global.t("hl_opSucc"));
      setTimeout(() => {
        closeToast();
        location.reload();
      }, 2500);
    })
    .on("error", (error, receipt) => {
      closeToast();
      showFailToast(i18n.global.t("hl_opFail"));
    });
}

function clickSetPartner() {
  if (setPartnerInput.value.length != 42) {
    showFailToast(i18n.global.t("hl_inputTrueAddr"));
    return;
  }

  let arr = [];
  arr.push(setPartnerInput.value);

  conContract.methods
    .setpartnerpoolAddress(arr)
    .send({
      from: address.value,
      gas: 600000,
      gasPrice: gasPrice.value,
    })
    .on("transactionHash", (hash) => {
      showLoadingToast({
        message: i18n.global.t("hl_waitLink"),
        forbidClick: true,
        duration: 0,
      });
    })
    .on("receipt", (receipt) => {
      closeToast();
      showSuccessToast(i18n.global.t("hl_opSucc"));
      setTimeout(() => {
        closeToast();
        location.reload();
      }, 2500);
    })
    .on("error", (error, receipt) => {
      closeToast();
      showFailToast(i18n.global.t("hl_opFail"));
    });
}

async function updataBossT() {
  if (!conContract || !bossTContract) {
    return;
  }

  if (bossTBalance.value < 100) {
    showFailToast(i18n.global.t("hl_noMoney"));
    return;
  }

  if (bossTlevel.value >= 13) {
    showFailToast("您已是最高级");
    return;
  }

  await getDotcAllowance(address.value, bossTContract, api.conAddress);

  try {
    const aa = conContract.methods.UP_P();

    const bb = await getEstimateGas(aa, address.value);

    await aa
      .send({
        from: address.value,
        gas: bb || 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          // location.reload();
          getBalance();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

async function getBossT() {
  if (!conContract) {
    return;
  }

  if (canGetBossT.value < 1) {
    showFailToast(i18n.global.t("hl_noMoney"));
    return;
  }

  try {
    const aa = conContract.methods.bosstoken();

    const bb = await getEstimateGas(aa, address.value);

    await aa
      .send({
        from: address.value,
        gas: bb || 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          // location.reload();
          getBalance();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}

function setNodeClick() {
  if (!inputSendAddr.value || inputSendAddr.value.length != 42) {
    showFailToast(i18n.global.t("hl_inputOpAddr"));
    return;
  }

  if (
    Number(inputAddr_num.value) > nodeNum.value ||
    Number(inputAddr_num.value) < 1 ||
    Number(inputAddr_num.value) % 1 != 0
  ) {
    let str = i18n.global.t("hl_input1_100") + nodeNum.value;
    showFailToast(str);
    return;
  }

  try {
    conContract.methods
      .setNode(inputSendAddr.value, Number(inputAddr_num.value))
      .send({
        from: address.value,
        gas: 600000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e) {
    console.log("e :>> ", e);
  }
}
</script>

<style scoped lang="less">
#mine {
  padding: 10px 3% 0;

  .top {
    width: 100%;
    padding: 3%;
    background: #f2f2f2;
    border-radius: 12px;
    box-shadow: 1px 1px 3px 1px #999;
    margin-bottom: 20px;

    div {
      display: flex;
      align-items: center;
      margin-bottom: 4px;

      p {
        color: black;
        margin-right: 6px;
      }

      h5 {
        color: #696565;
      }

      h6 {
        background: #1296db;
        border-radius: 4px;
        color: white;
        padding: 2px 12px;
      }
    }

    .copy {
      display: flex;
      align-items: center;
      justify-content: space-between !important;

      img {
        height: 36px;
        margin: 0 6px;
      }

      h5 {
        flex: 1;
        // overflow: hidden;
        // white-space: nowrap;
        // text-overflow: ellipsis;
        // text-align: left;
        margin: 0 8px;

        overflow-x: auto; /* 允许水平滚动 */
        white-space: nowrap; /* 防止内容换行 */
        scrollbar-width: none; /* Firefox 隐藏滚动条 */
        -ms-overflow-style: none; /* IE/Edge 隐藏滚动条 */
      }
    }
  }

  .coin {
    width: 100%;
    padding: 3%;
    background: #f2f2f2;
    border-radius: 12px;
    box-shadow: 1px 1px 3px 1px #999;
    margin-bottom: 20px;

    .head {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 20px;

      div {
        display: flex;
        align-items: center;
        flex-direction: column;
      }

      .left {
        display: flex;
        align-items: center;
        flex-direction: row;

        i {
          font-weight: bold;
          color: #dbbb97;
        }

        img {
          height: 40px;
          margin-right: 8px;
        }
      }
    }

    .btn {
      display: flex;
      align-items: center;
      justify-content: space-between;

      p {
        background: #1296db;
        width: 33%;
        text-align: center;
        height: 40px;
        line-height: 40px;
        color: white;
        border-radius: 6px;
      }

      h5 {
        color: #1296db;
      }
      h4 {
        background: #1296db;
        width: 45%;
        text-align: center;
        height: 40px;
        line-height: 40px;
        color: white;
        border-radius: 6px;
      }
    }

    .inputA {
      display: flex;

      align-items: center;
      justify-content: space-between;

      .left_input {
        flex: 1;
        margin-right: 12px;
        textarea {
          min-height: 52px;
          max-height: 72px;
          border: 1px solid black;
          border-radius: 4px;
        }

        input {
          height: 36px;
          max-width: 100%;
          border: 1px solid black;
          border-radius: 4px;
        }
      }

      div {
        p {
          display: flex;
          justify-content: center;
          align-items: center;

          background: #1296db;
          text-align: center;
          height: 40px;

          color: white;
          border-radius: 6px;
          margin: 2px 0;
          padding: 0 4px;
        }
      }
    }

    .btn2 {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .b_sel {
        background: #1296db;
        width: 20%;
        text-align: center;
        height: 40px;
        line-height: 40px;
        color: white;
        border-radius: 6px;
      }

      .b_un {
        width: 20%;
        text-align: center;
        height: 40px;
        line-height: 40px;
        color: #1296db;
        border-radius: 6px;
        border: 1px solid #1296db;
      }
    }
  }

  .list {
    width: 100%;
    padding: 3%;
    background: #f2f2f2;
    border-radius: 12px;
    // box-shadow: 1px 1px 3px 1px #999;

    h1 {
      width: 100%;
      font-size: 20px;
      text-align: center;
      margin-bottom: 12px;
    }

    .cell {
      margin-bottom: 12px;
      background: #f2f2f2;
      .line1 {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 8px;
        .addr {
          // width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 0 3%;
          p {
            margin-right: 12px;
            // white-space: pre-wrap;
            // word-break: break-all;
            // word-wrap: break-word;
          }

          img {
            height: 20px;
          }
          i {
            color: #dbbb97;
            font-size: 17px;
            font-weight: bold;
          }
        }

        .funBtn {
          display: flex;
          align-items: center;

          p {
            color: #1296db;
            border: 1px solid #1296db;
            height: 32px;
            line-height: 32px;
            border-radius: 4px;
            padding: 0 18px;
            margin-right: 12px;
          }
        }
        h5 {
          height: 32px;
          line-height: 32px;
          border-radius: 4px;
          background: #1296db;
          padding: 0 18px;
          color: white;
        }
      }
    }

    .nodata {
      img {
        width: 30%;
        margin: 24px auto;
      }

      p {
        color: #1296db;
        text-align: center;
        width: 100%;
      }
    }
  }

  .level_pop {
    border-radius: 18px;
    overflow: hidden;
    width: 100%;
    margin: 0 auto;
    position: relative;

    .urlText {
      height: 20px;
      text-align: center;
      margin: 36px 0 0;
    }
    .qrcodeView {
      padding: 0 36px 36px;
      img {
        margin: 0 auto !important;
      }
    }

    .urlText {
      height: 40px;
      line-height: 20px;
      text-align: center;
    }

    .back {
      position: absolute;
      right: 4px;
      top: 4px;
      height: 40px;
      width: 40px;
    }
    .level {
      width: 100%;
      background: #d9d9d9;
      padding: 12px;

      input {
        border: 1px solid black;
        width: 100%;
        height: 44px;
        margin: 12px 0;
      }

      h4 {
        color: #1296db;
        margin-bottom: 12px;
      }

      h1 {
        font-size: 18px;
        text-align: center;
        margin-bottom: 20px;
      }

      h3 {
        text-align: center;
        margin-bottom: 24px;
      }

      .btn {
        display: flex;
        align-items: center;
        justify-content: space-between;

        h5 {
          border: 1px solid #1296db;
          width: 20%;
          border-radius: 4px;
          color: #1296db;
          text-align: center;
        }

        p {
          background: #1296db;
          width: 20%;
          border-radius: 4px;
          color: white;
          text-align: center;
        }
      }

      .sel_1 {
        margin-bottom: 140px;
      }

      .sel_2 {
        margin-bottom: 20px;
      }
      .choose {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 12px;
        height: 44px;
        border: 1px solid black;
        line-height: 44px;
        background: white;
        position: relative;

        p {
          flex: 1;
        }
        .selectList {
          position: absolute;
          left: 0;
          top: 44px;
          width: 100%;
          max-height: 120px;
          background: white;
          overflow: auto;
          .sel_cell {
            height: 30px;
            line-height: 30px;
            border-bottom: 1px solid #d9d9d9;
            padding: 0 12px;
          }
        }

        img {
          height: 36px;
        }
      }
    }

    .levelBtn {
      display: flex;
      align-content: center;
      justify-content: space-between;
      margin-bottom: 12px;
      p {
        background: #1296db;
        color: white;
        padding: 0 24px;
        border-radius: 8px;
        line-height: 36px;
        height: 36px;
      }
      h5 {
        color: #1296db;
        border: 1px solid #1296db;
        padding: 0 24px;
        border-radius: 8px;
        line-height: 36px;
        height: 36px;
      }
    }
  }
}
</style>
